package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.MachineFilterEntity;
import com.sgb.link.entity.PublicTypeEntity;
import com.sgb.kjwl.model.entity.user.AttrEntity;
import com.sgb.kjwl.view.ui.adapter.device.FilterAdapter;
import com.sgb.kjwl.view.ui.adapter.self.publish.DeviceArrAdapter;
import com.sgb.kjwl.view.ui.adapter.self.publish.MachinePublishTypeAdapter;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;
import java.util.Map;

public class MachineTypePopWindow extends BasePopWindow {
    private String tag;
    private LinearLayout rootLayout;
    private MaxRecyclerView recyclerView;
    private MaxRecyclerView childView;
    private MaxRecyclerView attView;
    private TextView okView;
    private TextView resetView;
    private FilterResultClick filterResultClick;
    private List<PublicTypeEntity> machineTypeEntities;
    private List<PublicTypeEntity> publicTypeEntityChildList;
    private List<MachineFilterEntity> arrtList;
    private MachinePublishTypeAdapter machinePublishTypeAdapter;
    private MachinePublishTypeAdapter machinePublishTypeChildAdapter;
    private DeviceArrAdapter attadapter;
    private FilterAdapter filterAdapter;
    private List<String> noList;
    private Map map;
    //第一级分类编号
    private String pNo = "";
    //第二级类型编号
    private String cNo = "";
    //设备属性
    private String arrNo = "";
    //内容
    private String content = "";
    private String content1 = "";
    private String content2 = "";
    private String content3 = "";

    private boolean isReset = false;

    public interface FilterResultClick {
        void result(String key1, String key2, String key3);

        void dimiss();

        void queryChild(String pNo, String cNo, int position);
    }

    public void setFilterResultClick(FilterResultClick filterResultClick) {
        this.filterResultClick = filterResultClick;
    }

    public MachineTypePopWindow(Context context) {
        super(context, DIR_DEFUALT);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_publish_type;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        rootLayout = holder.getPop().findViewById(R.id.root_layout);
        recyclerView = holder.getPop().findViewById(R.id.filter_list);
        attView = holder.getPop().findViewById(R.id.att_list);
        childView = holder.getPop().findViewById(R.id.child_list);
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if (filterResultClick != null) {
                    content = content1 + "-" + content2;
                    if (isReset) {
                        isReset = false;
                        filterResultClick.result(pNo, cNo, "");
                    } else {
                        filterResultClick.result(pNo, cNo, content);
                    }

                }

            }
        });
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //第一级分类编号
                pNo = "";
                //第二级类型编号
                cNo = "";
                //内容
                content = "";
                content1 = "";
                content2 = "";
                isReset = true;
                rootLayout.removeAllViews();
                machinePublishTypeAdapter.setDefItem(-1);
                machinePublishTypeChildAdapter.setDefItem(-1);
            }
        });
        machinePublishTypeAdapter = new MachinePublishTypeAdapter(mContext, null);
        machinePublishTypeAdapter.setItemSelect(true);
        machinePublishTypeAdapter.setDefItem(-1);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(machinePublishTypeAdapter);
        machinePublishTypeAdapter.setOnItemClick(new AdapterOnItemClick<PublicTypeEntity>() {
            @Override
            public void onItemClick(PublicTypeEntity publicTypeEntity, int position) {
                pNo = publicTypeEntity.getNo();
                machinePublishTypeChildAdapter.setDefItem(0);
                content1 = publicTypeEntity.getName();
                if (filterResultClick != null) {
                    filterResultClick.queryChild(pNo, "", 1);
                }
            }
        });

        machinePublishTypeChildAdapter = new MachinePublishTypeAdapter(mContext, null);
        machinePublishTypeChildAdapter.setItemSelect(true);
        machinePublishTypeChildAdapter.setDefItem(-1);
        childView.setLayoutManager(new GridLayoutManager(mContext, 3));
        childView.setAdapter(machinePublishTypeChildAdapter);
        machinePublishTypeChildAdapter.setOnItemClick(new AdapterOnItemClick<PublicTypeEntity>() {
            @Override
            public void onItemClick(PublicTypeEntity publicTypeEntity, int position) {
                cNo = publicTypeEntity.getNo();
                content2 = publicTypeEntity.getName();
                if (filterResultClick != null) {
                    filterResultClick.queryChild(pNo, cNo, 2);
                }

            }
        });

        attadapter = new DeviceArrAdapter(mContext, null);
        attadapter.setItemSelect(true);
        attadapter.setDefItem(-1);
        attView.setLayoutManager(new GridLayoutManager(mContext, 3));
        attView.setAdapter(attadapter);
        attadapter.setOnItemClick(new AdapterOnItemClick<AttrEntity<List<AttrEntity>>>() {
            @Override
            public void onItemClick(AttrEntity<List<AttrEntity>> attrEntity, int position) {
                arrNo = attrEntity.getNo();
                content3 = attrEntity.getName();
            }
        });
    }

    public void setData(List<PublicTypeEntity> machineTypeEntities) {
        this.machineTypeEntities = machineTypeEntities;
        machinePublishTypeAdapter.setDatas(machineTypeEntities);
    }

    public void setChildData(List<PublicTypeEntity> machineTypeEntities) {
        this.publicTypeEntityChildList = machineTypeEntities;
        machinePublishTypeChildAdapter.setDatas(machineTypeEntities);
        cNo = publicTypeEntityChildList.get(0).getNo();
        content2 = publicTypeEntityChildList.get(0).getName();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (filterResultClick != null) {
            filterResultClick.dimiss();
        }
    }
}
